﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// shiming 的摘要说明
    /// </summary>
    public class shiming : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "shiming1_add": //提交评论
                    shiming1_add(context);
                    break;
                case "shiming2_add": //删除供应信息
                    shiming2_add(context);
                    break;
                case "shiming_info": //删除供应信息
                    shiming_info(context);
                    break;
            }
        }
        #region 提交个人实名认证===========================
        private void shiming1_add(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                Utils.WriteTextLog("shiming1_add", "username:" + user_name + ",pwd:" + user_pwd + "您的登录信息有误！", DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                Utils.WriteTextLog("shiming1_add", "username:" + user_name + ",pwd:" + user_pwd + "您的登录信息有误！", DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            BLL.shiming bll = new BLL.shiming();
            Model.shiming model = new Model.shiming();

            string _realname = DTRequest.GetFormString("txtRealName");
            string _mobile = DTRequest.GetFormString("txtMobile");
            string _shenfenzheng = DTRequest.GetFormString("txtShenfenzheng");

            if (string.IsNullOrEmpty(_realname))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请填写您的真实姓名！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_mobile))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请填写您的手机号！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_shenfenzheng))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请填写您的身份证号！\"}");
                return;
            }

            model.type_id = 1;
            model.user_id = userModel.id;
            model.real_name = _realname;
            model.mobile = _mobile;
            model.shenfenzheng_num = _shenfenzheng;
            model.add_time = DateTime.Now;
            model.status = 1;  //将状态改为 待审核

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.shiming_albums> ls = new List<Model.shiming_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    //string filePath = FileSave(context, myFile, false);
                    //获取文件信息
                    string fileName = myFile.FileName;
                    byte[] byteData = FileHelper.ConvertStreamToByteBuffer(myFile.InputStream); //获取文件流
                    //开始上传
                    string remsg = new UpLoad().FileSaveAs(byteData, fileName, true, true);//20190415 水印  
                    Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(remsg);

                    string thumb_path = dic["thumb"].ToString(); //取得上传后的路径
                    string original_path = dic["path"].ToString();

                    ls.Add(new Model.shiming_albums { original_path = original_path, thumb_path = thumb_path });
                }
                model.albums = ls;
            }
            #endregion

            int result = bll.Add(model);
            if (result > 0)
            {
                new BLL.users().UpdateField(userModel.id, "shiming_status=1,shiming_id=" + result);
                context.Response.Write("{\"status\": 1, \"msg\": \"实名认证信息提交成功，请等待管理员审核！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 提交企业实名认证===========================
        private void shiming2_add(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                Utils.WriteTextLog("shiming2_add", "username:" + user_name + ",pwd:" + user_pwd + "您的登录信息有误，请退出系统后重新登录！", DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                Utils.WriteTextLog("shiming2_add", "username:" + user_name + ",pwd:" + user_pwd + "您的登录信息有误，请退出系统后重新登录！", DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            BLL.shiming bll = new BLL.shiming();
            Model.shiming model = new Model.shiming();

            string _companyname = DTRequest.GetFormString("txtCompanyName");
            string _zhizhaonum = DTRequest.GetFormString("txtZhizhaoNum");
            string _realname = DTRequest.GetFormString("txtRealName");
            string _mobile = DTRequest.GetFormString("txtMobile");
            string _shenfenzheng = DTRequest.GetFormString("txtShenfenzheng");
            if (string.IsNullOrEmpty(_companyname))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请填写您的公司名称！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_zhizhaonum))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请填写您的公司的执照号码！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_realname))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请填写您的法人姓名！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_mobile))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请填写您的手机号！\"}");
                return;
            }
          

            model.type_id = 2;
            model.user_id = userModel.id;
            model.company_name = _companyname;
            model.zhizhao_num = _zhizhaonum;
            model.real_name = _realname;
            model.mobile = _mobile;
            model.shenfenzheng_num = _shenfenzheng;
            model.add_time = DateTime.Now;
            model.status = 1;  //将状态改为 待审核

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.shiming_albums> ls = new List<Model.shiming_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    //string filePath = FileSave(context, myFile, false);
                    //获取文件信息
                    string fileName = myFile.FileName;
                    byte[] byteData = FileHelper.ConvertStreamToByteBuffer(myFile.InputStream); //获取文件流
                    //开始上传
                    string remsg = new UpLoad().FileSaveAs(byteData, fileName, true, true);//20190415 水印
                    Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(remsg);

                    string thumb_path = dic["thumb"].ToString(); //取得上传后的路径
                    string original_path = dic["path"].ToString();

                    ls.Add(new Model.shiming_albums { original_path = original_path, thumb_path = thumb_path });
                }
                model.albums = ls;
            }
            #endregion

            int result = bll.Add(model);
            if (result > 0)
            {
                new BLL.users().UpdateField(userModel.id, "shiming_status=1,shiming_id=" + result);
                context.Response.Write("{\"status\": 1, \"msg\": \"实名认证信息提交成功，请等待管理员审核！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 获取实名认证信息=============================
        private void shiming_info(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                Utils.WriteTextLog("shiming_info", "username:" + user_name + ",pwd:" + user_pwd + "您的登录信息有误，请退出系统后重新登录！", DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                Utils.WriteTextLog("shiming_info", "username:" + user_name + ",pwd:" + user_pwd + "您的登录信息有误，请退出系统后重新登录！", DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            StringBuilder strTxt = new StringBuilder();

            strTxt.Append("{");
            strTxt.Append("\"shiming_status\":" + userModel.shiming_status);
            strTxt.Append(",\"shiming_id\":" + userModel.shiming_id);

            if (userModel.shiming_id > 0)
            {
                int _shiming_type = new BLL.shiming().GetModel(userModel.shiming_id).type_id;
                strTxt.Append(",\"type_id\":" + _shiming_type);
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}